package com.stox.ui.swing.widget.wrapper;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JPanel;

public class JRoundPanel extends JPanel {
	private static final long serialVersionUID = -6275340533176048334L;
	/** Stroke size. it is recommended to set it to 1 for better view */
    protected int strokeSize = 1;
    /** Color of shadow */
    protected Color shadowColor = Color.black;
    /** Sets if it drops shadow */
    protected boolean shady = true;
    /** Sets if it has an High Quality view */
    protected boolean highQuality = true;
    /** Double values for Horizontal and Vertical radius of corner arcs */
    protected Dimension arcs = new Dimension(20, 20);
    /** Distance between shadow border and opaque panel border */
    protected int shadowGap = 5;
    /** The offset of shadow.  */
    protected int shadowOffset = 4;
    /** The transparency value of shadow. ( 0 - 255) */
    protected int shadowAlpha = 150;
	
    protected RoundRectangle2D clip = new RoundRectangle2D.Double();
    
    public JRoundPanel() {
		super();
		setOpaque(false);
	}
    
    public JRoundPanel(int strokeSize, Color shadowColor, boolean shady,
			boolean highQuality, Dimension arcs, int shadowGap,
			int shadowOffset, int shadowAlpha) {
		this();
		this.strokeSize = strokeSize;
		this.shadowColor = shadowColor;
		this.shady = shady;
		this.highQuality = highQuality;
		this.arcs = arcs;
		this.shadowGap = shadowGap;
		this.shadowOffset = shadowOffset;
		this.shadowAlpha = shadowAlpha;
	}

	public JRoundPanel(int strokeSize, Color shadowColor, boolean shady,
			Dimension arcs) {
		this();
		this.strokeSize = strokeSize;
		this.shadowColor = shadowColor;
		this.shady = shady;
		this.arcs = arcs;
	}

	public JRoundPanel(boolean shady, Dimension arcs){
		this();
		this.shady = shady;
    	this.arcs = arcs;
    }
    
	
	public JRoundPanel(Dimension arcs){
		this();
    	this.arcs = arcs;
    }
    
    @Override
    protected void paintComponent(Graphics g) {
       //super.paintComponent(g);  
        int width = getWidth();
        int height = getHeight();
        int shadowGap = this.shadowGap;
        Color shadowColorA = new Color(shadowColor.getRed(), 
	shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha);
        Graphics2D graphics = (Graphics2D) g;

       
        
        if (highQuality) {
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
			RenderingHints.VALUE_ANTIALIAS_ON);
        }

        if (shady) {
            graphics.setColor(shadowColorA);
            graphics.fillRoundRect(
                    shadowOffset,// X position
                    shadowOffset,// Y position
                    width - strokeSize - shadowOffset, // width
                    height - strokeSize - shadowOffset, // height
                    arcs.width, arcs.height);// arc Dimension
        } else {
            shadowGap = 1;
        }

        //Draws the rounded opaque panel with borders.
        graphics.setColor(getBackground());
        graphics.fillRoundRect(0, 0, width - shadowGap, 
		height - shadowGap, arcs.width, arcs.height);
        graphics.setColor(getForeground());
        graphics.setStroke(new BasicStroke(strokeSize));
        graphics.drawRoundRect(0, 0, width - shadowGap, 
		height - shadowGap, arcs.width, arcs.height);

        //Sets strokes to default, is better.
        graphics.setStroke(new BasicStroke());
        clip.setRoundRect(getX(), getY(), getWidth(), getHeight(), arcs.getWidth(), arcs.getHeight());
        g.setClip(clip);
    }
}
